package com.hj.interceptor;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hj.dao.MenuMapper;
import com.hj.dao.RoleMenuMapper;
import com.hj.dao.StuRoleMapper;
import com.hj.pojo.Menu;
import com.hj.pojo.MenuExample;
import com.hj.pojo.RoleMenu;
import com.hj.pojo.RoleMenuExample;
import com.hj.pojo.StuRole;
import com.hj.pojo.StuRoleExample;
import com.hj.pojo.StuRoleExample.Criteria;
import com.hj.pojo.Student;
import com.hj.service.IMenuService;
import com.hj.vo.Result;

@Component
public class LoginInterceptor2 implements HandlerInterceptor {
	
	ObjectMapper om = new ObjectMapper();
	@Autowired
	StuRoleMapper stuRoleMapper;
	@Autowired
	RoleMenuMapper roleMenuMapper;
	@Autowired
	IMenuService menuService;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		
		HttpSession session = request.getSession();
		response.setCharacterEncoding("utf-8");
		
		Object uid = session.getAttribute("sessionUid");
		
		if (uid == null) {
			Result result = new Result();
	        result.setCode(201);
	        result.setMsg("请登录");
			response.getWriter().append(om.writeValueAsString(result));
			return false;
		}
		
		// 查询用户角色表
		StuRoleExample srEx = new StuRoleExample();
		Criteria criteria = srEx.createCriteria();
		criteria.andSidEqualTo(uid.toString());
		List<StuRole> srList = stuRoleMapper.selectByExample(srEx);
		List<Integer> rids = srList.stream().map(sr -> sr.getRid()).distinct().collect(Collectors.toList());
		
		// 查询角色权限表
		RoleMenuExample rmEx = new RoleMenuExample();
		com.hj.pojo.RoleMenuExample.Criteria rmCriteria = rmEx.createCriteria();
		rmCriteria.andRidIn(rids);
		List<RoleMenu> rmList = roleMenuMapper.selectByExample(rmEx);
		List<Integer> mids = rmList.stream().map(rm -> rm.getMid()).distinct().collect(Collectors.toList());
		
		// 获取菜单表记录
		MenuExample mEx = new MenuExample();
		com.hj.pojo.MenuExample.Criteria mCriteria = mEx.createCriteria();
		mCriteria.andIdIn(mids);
		
		List<Menu> menuList = menuService.findAll(mEx);
		
		// 获取当前访问的路径的
		String servletPath = request.getServletPath();
		System.out.println(servletPath);
		
		boolean flag = false;
		// 比对是否存在角色权限列表中
		for (Menu menu : menuList) {
			if (servletPath.equals(menu.getPath())) {
				flag = true;
				break;
			}
		}
		
		if (!flag) {
			Result result = new Result();
			result.setCode(202);
			result.setMsg("无权限");
			response.getWriter().append(om.writeValueAsString(result));
			return false;
		}
		
		return true;
	}
	
}
